package com.bite.friend.manager;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bit.common.redis.service.RedisService;
import com.bite.common.core.constants.CacheConstants;
import com.bite.common.core.domain.BaseEntity;
import com.bite.friend.domain.user.UserSubmit;
import com.bite.friend.mapper.user.UserSubmitMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserSubmitCacheManager {

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserSubmitMapper userSubmitMapper;

    public UserSubmit getUserSubmit(Long userId, Long questionId, Long examId, String currentTime) {
        String userSubmitKey = getUserSubmitKey(userId, questionId, examId);
        List<UserSubmit> userSubmitList = redisService.getCacheListByRange(userSubmitKey, 0, -1, UserSubmit.class);
        if (CollectionUtil.isEmpty(userSubmitList)) {
            refreshUserSubmitList(userId, questionId, examId, currentTime);
            userSubmitList = redisService.getCacheListByRange(userSubmitKey, 0, -1, UserSubmit.class);
        }
        return CollectionUtil.isEmpty(userSubmitList) ? null : userSubmitList.get(0);
    }

    public void refreshUserSubmitList(Long userId, Long questionId, Long examId, String currentTime) {
        List<UserSubmit> userSubmits = userSubmitMapper.selectList(new LambdaQueryWrapper<UserSubmit>()
                .eq(UserSubmit::getUserId, userId)
                .eq(UserSubmit::getQuestionId, questionId)
                .eq(examId != null, UserSubmit::getExamId, examId)
                .isNull(examId == null, UserSubmit::getExamId)
                .orderByAsc(BaseEntity::getCreateTime)
        );
        if (CollectionUtil.isEmpty(userSubmits)) return;
        String userSubmitKey = getUserSubmitKey(userId, questionId, examId);
        redisService.rightPushAll(userSubmitKey, userSubmits);
    }

    public String getSubmitMainCode(Long userId, Long questionId, Long examId, String currentTime) {
        UserSubmit userSubmit = getUserSubmit(userId, questionId, examId, currentTime);
        if (userSubmit == null) return "";
        String programCode = userSubmit.getProgramCode();
        String result;
        int index = programCode.indexOf("public static void main");
        result = programCode.substring(0, index - 1) + "}";
        return result;
    }

    private String getUserSubmitKey(UserSubmit userSubmit) {
        return getUserSubmitKey(userSubmit.getUserId(),
                userSubmit.getQuestionId(), userSubmit.getExamId());
    }

    private String getUserSubmitKey(Long userId, Long questionId, Long examId) {
        String result = CacheConstants.USER_SUBMIT_LIST
                + userId + CacheConstants.SEPARATOR + questionId;
        if (examId != null) {
            result = result + CacheConstants.SEPARATOR + examId;
        }
        return result;
    }
}
